#include "MainForm.h"
#include "MyDownloader.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Xml;
using namespace Osp::Base::Utility;


MainForm::MainForm(void) {
}

MainForm::~MainForm(void) {
}

bool MainForm::Initialize() {
	// Construct an XML form
	Construct(L"IDF_MAINFORM");

	return true;
}

result MainForm::OnInitializing(void) {
	result r = E_SUCCESS;

	__pScrollPanel1 = static_cast<ScrollPanel*>(this->GetControl("SCROLLPANEL1"));
	__pScrollPanel2 = static_cast<ScrollPanel*>(this->GetControl("SCROLLPANEL2"));
	__pScrollPanel3 = static_cast<ScrollPanel*>(this->GetControl("SCROLLPANEL3"));
	__pScrollPanel2->SetShowState(false);
	__pScrollPanel3->SetShowState(false);

	SetOptionkeyActionId(199);
	AddOptionkeyActionListener(*this);
	__pOptionsMenu = new OptionMenu();
	__pOptionsMenu->Construct();
	__pOptionsMenu->AddItem(L"Refresh", 200);
	__pOptionsMenu->AddActionEventListener(*this);

	Tab* __pTab = GetTab();
	if (__pTab) {
		__pTab->AddItem(L"Lista", ID_EGY);
		__pTab->AddItem(L"Letoltesek", ID_KETTO);
		__pTab->AddItem(L"Beallitasok", ID_HAROM);
		__pTab->AddActionEventListener(*this);
	}

	__pMyList = static_cast<CustomList *>(__pScrollPanel1->GetControl(L"MYLIST"));
	__pMyList->AddCustomItemEventListener(*this);
	__pCustomListItemFormat = new CustomListItemFormat();
	__pCustomListItemFormat->Construct();
	__pCustomListItemFormat->AddElement(0, Osp::Graphics::Rectangle(10,10,470,40),30, Osp::Graphics::Color::COLOR_BLACK, Osp::Graphics::Color::COLOR_GREY);
	__pCustomListItemFormat->AddElement(1, Osp::Graphics::Rectangle(10,50,470,80),30, Osp::Graphics::Color::COLOR_BLACK, Osp::Graphics::Color::COLOR_GREY);
	__pCustomListItemFormat->SetElementEventEnabled(0, true);
	__pCustomListItemFormat->SetElementEventEnabled(1, true);

	mydownloader.Download("http://mobilarena.hu/dl/cnt/2011-01/68925/lista.xml","/home/lista.xml");

	__pMyList->RemoveAllItems();
	ParseAndWrite();

	return r;
}

result MainForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch (actionId) {
	case ID_EGY: {
		__pMyList->RemoveAllItems();
		ParseAndWrite();
		__pScrollPanel1->SetShowState(true);
		__pScrollPanel2->SetShowState(false);
		__pScrollPanel3->SetShowState(false);
        this->RequestRedraw(true);
		AppLog("0\n");
	}
		break;
	case ID_KETTO: {
		__pScrollPanel1->SetShowState(false);
		__pScrollPanel2->SetShowState(true);
		__pScrollPanel3->SetShowState(false);
        this->RequestRedraw(true);
		AppLog("1\n");
	}
		break;
	case ID_HAROM: {
		__pScrollPanel1->SetShowState(false);
		__pScrollPanel2->SetShowState(false);
		__pScrollPanel3->SetShowState(true);
        this->RequestRedraw(true);
		AppLog("2\n");
	}
		break;
	case 199:{
		__pOptionsMenu->SetShowState(true);
		__pOptionsMenu->Show();
	}
	break;
	case 200:{
		mydownloader.Download("http://mobilarena.hu/dl/cnt/2011-01/68925/lista.xml","/home/lista.xml");
		__pMyList->RemoveAllItems();
		ParseAndWrite();
	}
		break;
	default:
		break;
	}
}

void MainForm::ParseAndWrite()
{
		// a dokumentum beolvasasa
		doc = xmlReadFile("/Home/lista.xml", NULL, 0);
		if(doc==NULL)
		{
			AppLog("xml dokumentum betoltesi problema");
		}

		/* kontextus felepitese */
		xpathContext = xmlXPathNewContext(doc);
		if(xpathContext == NULL) {
			AppLog("context problema");
			xmlFreeDoc(doc);
			//return(E_IO);
		}

		/* xpath kifejezes kiertekelese */
		xpathObjEloado = xmlXPathEvalExpression((xmlChar*)"//eloado", xpathContext);
		xpathObjCim = xmlXPathEvalExpression((xmlChar*)"//cim", xpathContext);
		if(xpathObjEloado == NULL || xpathObjCim == NULL) {
			AppLog("xpath kiertekelesi problema");
			xmlXPathFreeContext(xpathContext);
			xmlFreeDoc(doc);
		}

		/*eredmenyhalmaz bejarasa*/
		int size;
		int i;
		String eloado;
		String cim;
		size = (xpathObjEloado->nodesetval) ? xpathObjEloado->nodesetval->nodeNr : 0;
		AppLog("%d",size);
		for(i = 0; i <size; i++) {
			StringUtil::Utf8ToString((char*)xpathObjEloado->nodesetval->nodeTab[i]->children->content, eloado);
			StringUtil::Utf8ToString((char*)xpathObjCim->nodesetval->nodeTab[i]->children->content, cim);
			CustomListItem * newItem = new CustomListItem();
			newItem->Construct(80);
			newItem->SetItemFormat(*__pCustomListItemFormat);
			newItem->SetElement(0, eloado);
			newItem->SetElement(1, cim);
			__pMyList->AddItem(*newItem,0);
			__pMyList->SetItemEnabled(i,true);
		}

		this->RequestRedraw(true);

		xmlFreeDoc(doc);
		xmlCleanupParser();
}



void MainForm::OnItemStateChanged(const Osp::Ui::Control& source, int index, int itemId, Osp::Ui::ItemStatus status) {
	//AppLog("%d %d",index,itemId);
}



void MainForm::OnItemStateChanged(const Osp::Ui::Control& source, int index, int itemId, int elementId, Osp::Ui::ItemStatus status) {
	/*switch (elementId) {
	case DELETE_LISTENER: {
		AppLog("delete called, row: %d .", index);
		}
		break;
	default:
		break;
	}*/AppLog("%d %d",index,itemId);
}
